package com.hl.qzzpbackend.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hl.qzzpbackend.exception.BusinessException;
import com.hl.qzzpbackend.exception.ErrorCode;
import com.hl.qzzpbackend.exception.ThrowUtils;
import com.hl.qzzpbackend.mapper.CompanyInfoMapper;
import com.hl.qzzpbackend.model.dto.companyInfo.CompanyInfoAddRequest;
import com.hl.qzzpbackend.model.dto.companyInfo.CompanyInfoUpdateRequest;
import com.hl.qzzpbackend.model.dto.companyInfo.CompanyQueryRequest;
import com.hl.qzzpbackend.model.entity.CompanyInfo;
import com.hl.qzzpbackend.model.entity.IndustryType;
import com.hl.qzzpbackend.model.entity.User;
import com.hl.qzzpbackend.model.enums.*;
import com.hl.qzzpbackend.model.vo.CompanyInfoVO;
import com.hl.qzzpbackend.service.CompanyInfoService;
import com.hl.qzzpbackend.service.IndustryTypeService;
import com.hl.qzzpbackend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author helong
 *
 * 公司信息 业务层
 */
@Service
public class CompanyInfoServiceImpl extends ServiceImpl<CompanyInfoMapper, CompanyInfo>
    implements CompanyInfoService{

    @Resource
    private CompanyInfoMapper companyInfoMapper;

    @Resource
    private IndustryTypeService industryTypeService;
    @Autowired
    private UserService userService;

    /**
     * 添加公司信息
     * @param companyInfoAddRequest 添加公司信息请求类
     * @return 是否成功
     */
    @Override
    public boolean companyInfoAdd(CompanyInfoAddRequest companyInfoAddRequest) {
        Long industryTypeId = companyInfoAddRequest.getIndustryTypeId();
        Long userId = companyInfoAddRequest.getUserId();
        String name = companyInfoAddRequest.getName();
        Integer companySize = companyInfoAddRequest.getCompanySize();
        Integer companyType = companyInfoAddRequest.getCompanyType();
        Integer fundingStage = companyInfoAddRequest.getFundingStage();
        List<String> benefits = companyInfoAddRequest.getBenefits();
        Integer workDays = companyInfoAddRequest.getWorkDays();
        Integer overtime = companyInfoAddRequest.getOvertime();
        CompanySizeEnum companySizeEnum = CompanySizeEnum.getEnumByValue(companySize);
        CompanyTypeEnum companyTypeEnum = CompanyTypeEnum.getEnumByValue(companyType);
        FundingStageEnum fundingStageEnum = FundingStageEnum.getEnumByValue(fundingStage);
        WorkDaysEnum workDaysEnum = WorkDaysEnum.getEnumByValue(workDays);
        OverTimeEnum overTimeEnum = OverTimeEnum.getEnumByValue(overtime);

        // 参数校验
        this.validate(companySizeEnum, companyTypeEnum, fundingStageEnum, workDaysEnum, overTimeEnum,industryTypeId);

        long count = this.lambdaQuery().eq(CompanyInfo::getName, name).count();
        ThrowUtils.throwIf(count > 0,ErrorCode.PARAMS_ERROR,"公司名称已存在");
        long count1 = userService.lambdaQuery().eq(User::getId,userId).count();
        ThrowUtils.throwIf(count1 == 0,ErrorCode.PARAMS_ERROR,"用户不存在");


        CompanyInfo companyInfo = new CompanyInfo();
        BeanUtil.copyProperties(companyInfoAddRequest, companyInfo);
        String benefitsStr = JSONUtil.toJsonStr(benefits);
        companyInfo.setBenefits(benefitsStr);

        boolean result = this.save(companyInfo);
        if(!result){
            throw new RuntimeException("添加公司信息失败");
        }

        return true;
    }


    /**
     * 修改公司信息
     * @param companyInfoUpdateRequest 添加公司信息请求类
     * @return 是否成功
     */
    @Override
    public boolean companyInfoUpdate(CompanyInfoUpdateRequest companyInfoUpdateRequest) {
        Long id = companyInfoUpdateRequest.getId();
        String name = companyInfoUpdateRequest.getName();
        Integer companySize = companyInfoUpdateRequest.getCompanySize();
        Integer companyType = companyInfoUpdateRequest.getCompanyType();
        Integer fundingStage = companyInfoUpdateRequest.getFundingStage();
        List<String> benefits = companyInfoUpdateRequest.getBenefits();
        Integer workDays = companyInfoUpdateRequest.getWorkDays();
        Integer overtime = companyInfoUpdateRequest.getOvertime();
        Long industryTypeId = companyInfoUpdateRequest.getIndustryTypeId();
        CompanySizeEnum companySizeEnum = CompanySizeEnum.getEnumByValue(companySize);
        CompanyTypeEnum companyTypeEnum = CompanyTypeEnum.getEnumByValue(companyType);
        FundingStageEnum fundingStageEnum = FundingStageEnum.getEnumByValue(fundingStage);
        WorkDaysEnum workDaysEnum = WorkDaysEnum.getEnumByValue(workDays);
        OverTimeEnum overTimeEnum = OverTimeEnum.getEnumByValue(overtime);

        // 参数校验
        this.validate(companySizeEnum, companyTypeEnum, fundingStageEnum, workDaysEnum, overTimeEnum,industryTypeId);
        long count = this.lambdaQuery().eq(CompanyInfo::getName, name).ne(CompanyInfo::getName,name).count();
        ThrowUtils.throwIf(count > 0,ErrorCode.PARAMS_ERROR,"公司名称已存在");

        CompanyInfo companyInfo = new CompanyInfo();
        BeanUtil.copyProperties(companyInfoUpdateRequest, companyInfo);
        String benefitsStr = JSONUtil.toJsonStr(benefits);
        companyInfo.setBenefits(benefitsStr);

        boolean result = this.save(companyInfo);
        if(!result){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"修改公司信息失败");
        }

        return true;
    }

    /**
     * 获取公司信息
     * @param user 登录用户
     * @return 是否成功
     */
    @Override
    public CompanyInfoVO getInfo(User user) {
        Long userId = user.getId();
        CompanyInfo companyInfo = this.lambdaQuery().eq(CompanyInfo::getUserId, userId).one();
        CompanyInfoVO companyInfoVO = new CompanyInfoVO();
        BeanUtil.copyProperties(companyInfo, companyInfoVO);
        List<String> Benefits = JSONUtil.toList(companyInfo.getBenefits(), String.class);
        companyInfoVO.setBenefits(Benefits);
        return companyInfoVO;
    }

    /**
     *
     * @param companyQueryRequest 公司信息查询请求
     * @return 公司信息查询条件构造器
     */
    @Override
    public LambdaQueryWrapper<CompanyInfo> getQueryWrapper(CompanyQueryRequest companyQueryRequest) {
        String name = companyQueryRequest.getName();
        Integer companySize = companyQueryRequest.getCompanySize();
        Integer companyType = companyQueryRequest.getCompanyType();
        return Wrappers.lambdaQuery(CompanyInfo.class)
                .eq(ObjUtil.isNotNull(companySize),CompanyInfo::getCompanySize,companySize)
                .eq(ObjUtil.isNotNull(companyType),CompanyInfo::getCompanyType,companyType)
                .like(StrUtil.isNotBlank(name),CompanyInfo::getName,name)
                .orderByDesc(CompanyInfo::getCreateTime);

    }

    /**
     * 获取公司信息列表
     * @param companyInfoList 公司信息列表
     * @return 公司信息列表
     */
    @Override
    public List<CompanyInfoVO> getCompanyInfoVOList(List<CompanyInfo> companyInfoList) {
        if(CollUtil.isEmpty(companyInfoList)){
            return Collections.emptyList();
        }
        return companyInfoList.stream().map(companyInfo -> {
            CompanyInfoVO companyInfoVO = new CompanyInfoVO();
            BeanUtil.copyProperties(companyInfo, companyInfoVO);
            List<String> Benefits = JSONUtil.toList(companyInfo.getBenefits(), String.class);
            companyInfoVO.setBenefits(Benefits);
            return companyInfoVO;
        }).collect(Collectors.toList());
    }

    /**
     * 参数校验
     * @param companySizeEnum 公司规模
     * @param companyTypeEnum 公司类型
     * @param fundingStageEnum 融资阶段
     * @param workDaysEnum 工作时间
     * @param overTimeEnum 加班时间
     * @param industryTypeId 行业类型id
     */
    @Override
    public void validate(
            CompanySizeEnum companySizeEnum,
            CompanyTypeEnum companyTypeEnum,
            FundingStageEnum fundingStageEnum,
            WorkDaysEnum workDaysEnum,
            OverTimeEnum overTimeEnum,
            Long industryTypeId) {
        // 参数校验
        ThrowUtils.throwIf(companySizeEnum == null, ErrorCode.PARAMS_ERROR,"公司规模不合法");
        ThrowUtils.throwIf(companyTypeEnum == null, ErrorCode.PARAMS_ERROR,"公司类型不合法");
        ThrowUtils.throwIf(fundingStageEnum == null, ErrorCode.PARAMS_ERROR,"融资阶段不合法");
        ThrowUtils.throwIf(workDaysEnum == null, ErrorCode.PARAMS_ERROR,"工作日不合法");
        ThrowUtils.throwIf(overTimeEnum == null, ErrorCode.PARAMS_ERROR,"加班不合法");

        long count = industryTypeService.lambdaQuery().eq(IndustryType::getId, industryTypeId).count();
        ThrowUtils.throwIf(count == 0,ErrorCode.PARAMS_ERROR,"行业类型不存在");
    }



}




